package com.cn.kehong.service;

import com.cn.kehong.api.IPlanYearDetailService;
import com.cn.kehong.dao.PlanYearDetailMapper;
import com.cn.kehong.domain.PlanYearDetail;
import com.cn.kehong.domain.PlanYearDetailExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PlanYearDetailServiceImpl implements IPlanYearDetailService {

    @Autowired
    private PlanYearDetailMapper planYearDetailMapper;

    @Override
    public List<PlanYearDetail> getPlanYearDetails(String planYearId) throws Exception {
        if(StringUtils.isEmpty(planYearId)){
            return Lists.newArrayList();
        }
        PlanYearDetailExample example  = new PlanYearDetailExample();
        example.createCriteria().andPlanYearIdEqualTo(planYearId).andStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("sort_index asc");
        List<PlanYearDetail> planYearDetails = planYearDetailMapper.selectByExample(example);
        return planYearDetails;
    }

    @Override
    public String addPlanYearDetail(List<PlanYearDetail> planYearDetails) throws Exception {
        if(CollectionUtils.isEmpty(planYearDetails)){
            return "数据为空！";
        }
        String planYearId = planYearDetails.get(0).getPlanYearId();
        if(StringUtils.isEmpty(planYearId)){
            return "主键为空！";
        }
        PlanYearDetailExample example  = new PlanYearDetailExample();
        example.createCriteria().andPlanYearIdEqualTo(planYearId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = planYearDetailMapper.countByExample(example);
        if(existCount > 0){
            return "数据已经存在!";
        }
        int count = planYearDetailMapper.batchInsertPlanYearDetail(planYearDetails);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updatePlanYearDetail(List<PlanYearDetail> planYearDetails) throws Exception {
        if(CollectionUtils.isEmpty(planYearDetails)){
            return "数据为空！";
        }
        String planYearId = planYearDetails.get(0).getPlanYearId();
        if(StringUtils.isEmpty(planYearId)){
            return "主键为空！";
        }
        PlanYearDetailExample example  = new PlanYearDetailExample();
        example.createCriteria().andPlanYearIdEqualTo(planYearId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = planYearDetailMapper.countByExample(example);
        if(existCount == 0){
            return "数据不存在!";
        }
        int count = planYearDetailMapper.deleteByExample(example);
        if(count < 0){
            return "删除数据异常！";
        }
        count = planYearDetailMapper.batchInsertPlanYearDetail(planYearDetails);
        if(count < 0){
            throw new Exception("批量更新异常！");
        }
        return CommonUtil.outStr(count);
    }
}
